package com.cedifie.lendapp.ui.loan.act

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Typeface
import android.os.CountDownTimer
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import com.cedifie.lendapp.R
import com.cedifie.lendapp.databinding.ActLoanApplySuccessBinding
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.mvp.RestartAct
import com.cedifie.lendapp.utils.event.CedEventType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2025/1/21 18:11
 * @Annotation：申贷成功等待页面
 */
class CedLoanApplySuccessAct: RestartAct(), CoroutineScope by MainScope() {
    lateinit var binding: ActLoanApplySuccessBinding
    //等待时长
    private var countdown = 9
    private var countdownTimer: MyCountDownTimer? = null

    override fun getCedRootView(): View {
        binding = ActLoanApplySuccessBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCedView() {
        setCedTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setCedTextTitle("")
        setCedBackBtnVisibility(View.INVISIBLE)
        setCedRightBtnVisibility(View.INVISIBLE)

        val textSMS = getString(R.string.loan_apply_wait_sms)
        val showText = String.format(getString(R.string.loan_apply_wait_tips),textSMS)

        val smsIndex = showText.indexOf(textSMS)
        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF0000")), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textWaitTips.text = spString
    }

    override fun initCedListener() {

    }

    override fun initCedData() {
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    class MyCountDownTimer(private val act: CedLoanApplySuccessAct, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            act.countdown = (millisUntilFinished / 1000).toInt()
            act.showApplyWaitTipsText()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            act.showApplyWaitComplete()
        }
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showApplyWaitTipsText(){
        binding.txtCountdown.text = "${countdown}s"
        showApplyWaitStepStatusView()
    }

    /**
     * 倒计时结束
     */
    private fun showApplyWaitComplete(){
        //发送更新总流程通知
        EventBus.getDefault().post(CedEventType.NOTICE.CED_PROCESS_REFRESH)
        launch(Dispatchers.Main){
            delay(500)
            //返回首页
            finish()
        }
    }

    /**
     * 显示状态
     */
    private fun showApplyWaitStepStatusView(){
        if (countdown in 7..9){
            binding.imgStep1.setImageResource(R.mipmap.icon_wait_step2)
            binding.txtStep1.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep2.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep2.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep3.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
            return
        }

        if (countdown in 4..6){
            binding.imgStep1.setImageResource(R.mipmap.icon_wait_step1)
            binding.txtStep1.setTextColor(Color.parseColor("#FFA9A9AC"))

            binding.imgStep2.setImageResource(R.mipmap.icon_wait_step2)
            binding.txtStep2.setTextColor(Color.parseColor("#FF000000"))

            binding.imgStep3.setImageResource(R.mipmap.icon_wait_countdown)
            binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
            return
        }
        binding.imgStep1.setImageResource(R.mipmap.icon_wait_step1)
        binding.txtStep1.setTextColor(Color.parseColor("#FFA9A9AC"))

        binding.imgStep2.setImageResource(R.mipmap.icon_wait_step1)
        binding.txtStep2.setTextColor(Color.parseColor("#FFA9A9AC"))

        binding.imgStep3.setImageResource(R.mipmap.icon_wait_step2)
        binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addCedEventAction(CedEventType.PAGE.CED_PAGE_LOAN_SMS_PREVIEW_START, "")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addCedEventAction(CedEventType.PAGE.CED_PAGE_LOAN_SMS_PREVIEW_END, "")
        if (countdownTimer != null){
            countdownTimer?.cancel()
            countdownTimer = null
        }
    }
}